package com.zcx.presenter;

import com.zcx.base.BasePresenter;
import com.zcx.base.BaseView;

import java.lang.ref.WeakReference;

/**
 * @author：zcx
 * @date:on 2018/3/6 0006 17:52
 */

public class AppPresenter<V extends BaseView> implements BasePresenter<V> {
    /*
    * View的弱引用
    */
    private WeakReference<V> viewRef;

    /**
     * 关联
     */
    @Override
    public void attachView(V view) {
        viewRef = new WeakReference<V>(view);
    }

    /**
     * 得到VIew
     * @return
     */
    public V getView() {
        return viewRef==null ? null : viewRef.get();
    }

    /**
     * 判断viewRef是否为空
     */
    public boolean isViewAttachd() {
        return viewRef!=null&&viewRef.get()!=null;
    }

    /**
     * 解除关联
     */
    @Override
    public void detachView() {
        if(viewRef!=null) {
            viewRef.clear();
            viewRef = null;
        }
    }
}
